package org.bing.engine.utility.helper;

import java.security.SecureRandom;
import java.util.concurrent.atomic.AtomicLong;

import org.bing.engine.utility.codec.Md5Helper;

public class GuidGenerator {
    private static final String PREFIX = HostHelper.getHostAddress() + "-" + System.currentTimeMillis() + "-" + System.nanoTime();
    private static final AtomicLong SNO = new AtomicLong();
    private static final SecureRandom random = new SecureRandom();

    /** use hex codec, fast but more length */
    public static String generate() {
        StringBuilder sb = new StringBuilder(64);
        sb.append(PREFIX).append("-").append(SNO.incrementAndGet()).append("-").append(System.nanoTime());
        return Md5Helper.calcMd5(sb.toString());
    }

    public static String random(int size) {
        StringBuilder sb = new StringBuilder(size);
        for (int i = 0; i < size; i++) {
            char c = (char) (97 + random.nextInt(26));// 97 - 122 (a-z)
            sb.append(c);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 50 * 1; i++) {
            System.out.println(GuidGenerator.random(5));
        }
        // GuidGenerator generator = new GuidGenerator();
        // long t1 = System.currentTimeMillis();
        // for (int i = 0; i < 50 * 10000; i++) {
        // System.out.println(GuidGenerator.generate());
        // GuidGenerator.generate();
        // }
        // System.out.println(System.currentTimeMillis() - t1);
    }
}
